package scparser.factory.ShaderFactory;

import java.awt.Color;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import lib.common.Constants;
import model.Scene;
import model.shader.ConstantShader;
import model.shader.Shader;
import scparser.ParserHelper;
import scparser.factory.primitiveFactory.FactoryException;

public class ConstantFactory extends ShaderFactory implements ShaderAbstractFactory{
	private Color color;

	public ConstantFactory(Scene scene) {
		super.scene = scene;
	}

	@Override
	public Shader create(List<String> block) throws FactoryException {
		String line = null;
		Iterator<String> it = block.iterator();

		try {
			while (it.hasNext()) {
				line = it.next().trim();

				if (line.equals(Constants.S_CLOSE)) {
					break;
				} else if (line.startsWith("name")) {
					super.name = ParserHelper.takeString(line, "name");
				} else if (line.startsWith("color")) {
					this.color = ParserHelper.getColor(line);
				}
			}
		} catch (IOException e) {
			throw new FactoryException(e.getMessage());
		} catch (FactoryException e) {
			throw new FactoryException(e.getMessage());
		}
		
		ConstantShader ret = new ConstantShader(name, color);
		if (scene.getShaders().contains(ret)) {
			throw new FactoryException("shader already exists");
		}
		return ret;
	}
}
